# 抓取任务：
# 1、分页抓取网页相关数据
# 2、根据行数据 下载网页pdf

import requests
import json

cookies = {
    'qgqp_b_id': '0b48e3bc368c61151376959a41521373',
    'websitepoptg_api_time': '1698713183795',
    'st_si': '62024988744053',
    'websitepoptg_show_time': '1698713183991',
    'st_asi': 'delete',
    'st_pvi': '65827667859447',
    'st_sp': '2023-10-31%2008%3A46%3A23',
    'st_inirUrl': 'https%3A%2F%2Fcn.bing.com%2F',
    'st_sn': '10',
    'st_psi': '20231031103106577-113300300989-5966743870',
    'JSESSIONID': 'DF5D65ECFA6EC314504EDDC5EF62964C',
}

headers = {
    'Accept': '*/*',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    # 'Cookie': 'qgqp_b_id=0b48e3bc368c61151376959a41521373; websitepoptg_api_time=1698713183795; st_si=62024988744053; websitepoptg_show_time=1698713183991; st_asi=delete; st_pvi=65827667859447; st_sp=2023-10-31%2008%3A46%3A23; st_inirUrl=https%3A%2F%2Fcn.bing.com%2F; st_sn=10; st_psi=20231031103106577-113300300989-5966743870; JSESSIONID=DF5D65ECFA6EC314504EDDC5EF62964C',
    'Referer': 'https://data.eastmoney.com/xg/xg/default.html',
    'Sec-Fetch-Dest': 'script',
    'Sec-Fetch-Mode': 'no-cors',
    'Sec-Fetch-Site': 'same-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

pageNumber = 2
response = requests.get(
    f'https://datacenter-web.eastmoney.com/api/data/v1/get?callback=jQuery112309701507943309216_1698713393671&sortColumns=APPLY_DATE%2CSECURITY_CODE&sortTypes=-1%2C-1&pageSize=50&pageNumber={pageNumber}&reportName=RPTA_APP_IPOAPPLY&columns=SECURITY_CODE%2CSECURITY_NAME%2CTRADE_MARKET_CODE%2CAPPLY_CODE%2CTRADE_MARKET%2CMARKET_TYPE%2CORG_TYPE%2CISSUE_NUM%2CONLINE_ISSUE_NUM%2COFFLINE_PLACING_NUM%2CTOP_APPLY_MARKETCAP%2CPREDICT_ONFUND_UPPER%2CONLINE_APPLY_UPPER%2CPREDICT_ONAPPLY_UPPER%2CISSUE_PRICE%2CLATELY_PRICE%2CCLOSE_PRICE%2CAPPLY_DATE%2CBALLOT_NUM_DATE%2CBALLOT_PAY_DATE%2CLISTING_DATE%2CAFTER_ISSUE_PE%2CONLINE_ISSUE_LWR%2CINITIAL_MULTIPLE%2CINDUSTRY_PE_NEW%2COFFLINE_EP_OBJECT%2CCONTINUOUS_1WORD_NUM%2CTOTAL_CHANGE%2CPROFIT%2CLIMIT_UP_PRICE%2CINFO_CODE%2COPEN_PRICE%2CLD_OPEN_PREMIUM%2CLD_CLOSE_CHANGE%2CTURNOVERRATE%2CLD_HIGH_CHANG%2CLD_AVERAGE_PRICE%2COPEN_DATE%2COPEN_AVERAGE_PRICE%2CPREDICT_PE%2CPREDICT_ISSUE_PRICE2%2CPREDICT_ISSUE_PRICE%2CPREDICT_ISSUE_PRICE1%2CPREDICT_ISSUE_PE%2CPREDICT_PE_THREE%2CONLINE_APPLY_PRICE%2CMAIN_BUSINESS%2CPAGE_PREDICT_PRICE1%2CPAGE_PREDICT_PRICE2%2CPAGE_PREDICT_PRICE3%2CPAGE_PREDICT_PE1%2CPAGE_PREDICT_PE2%2CPAGE_PREDICT_PE3%2CSELECT_LISTING_DATE%2CIS_BEIJING%2CINDUSTRY_PE_RATIO%2CINDUSTRY_PE%2CIS_REGISTRATION&quoteColumns=f2~01~SECURITY_CODE~NEWEST_PRICE&quoteType=0&filter=(APPLY_DATE%3E%272010-01-01%27)&source=WEB&client=WEB',
    cookies=cookies,
    headers=headers,
).text

# 截取 范围内容
response = response[(response.index("(") + 1):response.rindex(")")]

response_result = json.loads(response)

info_code_list = []

for index, item in enumerate(response_result['result']['data']):
    print(index, ' - ', item)
    info_code_list.append(item['INFO_CODE'])

number = int(input("输入编号，下载招股说明书/意向书:"))

pdf_url = f'https://pdf.dfcfw.com/pdf/H2_{info_code_list[number]}_1.pdf'
pdf_response = requests.get(url=pdf_url,cookies=cookies,headers=headers)

with open(f'{info_code_list[number]}_1.pdf','wb') as f:
    f.write(pdf_response.content)
    print("下载成功！")